package net.maku.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.convert.MainOrderConvert;
import net.maku.entity.MainOrderEntity;
import net.maku.service.MainOrderService;
import net.maku.query.MainOrderQuery;
import net.maku.vo.MainOrderVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
* 订单
*
* @author 小陈 
* @since 1.0.0 2025-03-17
*/
@RestController
@RequestMapping("danhao/mainOrder")
@Tag(name="订单")
@AllArgsConstructor
public class MainOrderController {
    private final MainOrderService mainOrderService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('danhao:mainOrder:page')")
    public Result<PageResult<MainOrderVO>> page(@ParameterObject @Valid MainOrderQuery query){
        PageResult<MainOrderVO> page = mainOrderService.pageList(query);
        return Result.ok(page);
    }

    @PostMapping("/uploadExcel")
    @Operation(summary = "上传excel批量匹配")
//    @PreAuthorize("hasAuthority('danhao:mainOrder:page')")
    public Result<List<MainOrderVO>> uploadExcel(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            return Result.error("请选择需要上传的文件");
        }
        List<MainOrderVO> mainOrderVOS = mainOrderService.importByExcel(file);
        return Result.ok(mainOrderVOS);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('danhao:mainOrder:info')")
    public Result<MainOrderVO> get(@PathVariable("id") Long id){
        MainOrderEntity entity = mainOrderService.getById(id);

        return Result.ok(MainOrderConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('danhao:mainOrder:save')")
    public Result<String> save(@RequestBody MainOrderVO vo){
        mainOrderService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('danhao:mainOrder:update')")
    public Result<String> update(@RequestBody @Valid MainOrderVO vo){
        mainOrderService.update(vo);

        return Result.ok();
    }

    @PostMapping("buy/{id}")
    @Operation(summary = "购买")
    @PreAuthorize("hasAuthority('danhao:mainOrder:buy')")
    public Result<String> buyOrder(@PathVariable("id") Long id){
        return mainOrderService.buy(id);
    }

    @PostMapping("buyList")
    @Operation(summary = "批量购买")
    @PreAuthorize("hasAuthority('danhao:mainOrder:buylist')")
    public Result<String> PurchaseList(@RequestBody List<Long> idList){
        return mainOrderService.purchaseList(idList);
    }
}